<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<script type="text/javascript">
    var departmentDataGrid;
    var departmentTree;
	var cuurname=null;
    $(function() {
    	//easyui tree树插件
        $('#departmentTree').tree({
            url : '${path }/admin/department/tree',
            parentField : 'pid',
            lines : true,
            onContextMenu: function(e, node){
            	$("#departmentId").val(node.id);
            	cuurname=node.text;
				e.preventDefault();
				//选中状态 
                $("#deprightmenu").menu("show", {
                    left: e.pageX,
                    top: e.pageY
                });
			},
           onClick : function(node) {
            	$(this).tree('toggle', node.target);
            	$('#departmentId').val(node.id);
            	$(".department").layout('panel','center').panel('setTitle','当前栏目：'+node.text+" 》 信息列表");
                departmentDataGrid.datagrid('load', {
                    parentid: node.id
                });
            }
        });
        

        departmentDataGrid = $('#departmentDataGrid').datagrid({
            url : '${path }/admin/department/dataGrid',
            fit : true,
            striped : true,
            rownumbers : true,
            pagination : true,
            singleSelect : true,
            idField : 'id',
            sortName : 'sequence',
	        sortOrder : 'asc',
            pageSize : 10,
            pageList : [ 10, 20, 30, 40, 50 ],
            columns : [ [ {
                width : '200',
                title : '院系名称',
                field : 'department_name',
                sortable : true,
                formatter : function(value, row, index) {
                 switch (row.grade) {
                    case 0:
                       var str = '';
                       str += row.department_name;
                       str += $.formatString('<span class="grid_cell_status" >1级选项</span>', row.id);
                    	return str;
                    case 1:
                       var str = '';
                       str += row.department_name;
                       return str;
                    }
                    
                }
            }, {
                width : '120',
                title : '联系电话',
                field : 'telephone',
                sortable : true
            },{
                width : '120',
                title : '邮箱',
                field : 'email',
                sortable : true
            }, {
                field : 'actions',
                title : '用户分配',
                width : 120,
                formatter : function(value, row, index) {
                    var str = '';
                    	if (row.grade==0) {
							<shiro:hasPermission name="/admin/department/assignuser">
                            str += $.formatString('<a href="javascript:void(0)" class="department-easyui-linkbutton-assign" data-options="plain:true,iconCls:\'fi-check icon-blue\'" onclick="assignUserFun(\'{0}\',\'{1}\');" >分配</a>', row.id,0);
                       		</shiro:hasPermission>
                       		<shiro:hasPermission name="/admin/department/userList">
                       		str += $.formatString('<a href="javascript:void(0)" class="department-easyui-linkbutton-user" data-options="plain:true,iconCls:\'fi-zoom-in icon-blue\'" onclick="assignUserList(\'{0}\',\'{1}\');" >查看</a>', row.id,0);
							</shiro:hasPermission>
						}
                    return str;
                }
            },{
                width : '50',
                title : '排序',
                field : 'sequence'
            },{
                width : '60',
                title : '状态',
                field : 'status',
                sortable : true,
                formatter : function(value, row, index) {
                    switch (value) {
                    case 0:
                        return '停用';
                    case 1:
                        return '正常';
                    }
                }
            }, {
                field : 'action',
                title : '操作',
                width : 190,
                formatter : function(value, row, index) {
                    var str = '';
                        <shiro:hasPermission name="/admin/department/edit">
                            str += $.formatString('<a href="javascript:void(0)" class="department-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="editDepartmentFun(\'{0}\',\'{1}\');" >编辑</a>', row.id,0);
                        </shiro:hasPermission>
                        <shiro:hasPermission name="/admin/department/delete">
                            str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                            str += $.formatString('<a href="javascript:void(0)" class="department-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red\'" onclick="deleteDepartmentFun(\'{0}\',\'{1}\');" >删除</a>', row.id,0);
                        </shiro:hasPermission>
                    return str;
                }
            }] ],
            onLoadSuccess:function(data){
            	$('.department-easyui-linkbutton-user').linkbutton({text:'查看'});
            	$('.department-easyui-linkbutton-assign').linkbutton({text:'分配'});
                $('.department-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.department-easyui-linkbutton-del').linkbutton({text:'删除'});
            },
            toolbar : '#departmentToolbar'
        });
    });
      //获取父id
    function getDepartmentId(){
    	var parentId=$("#departmentId").val();
    	return parentId;
    }  
    function addDepartmentFun() {
    	parent.addFrameTab('院系编辑','${path }/admin/department/add','');
    }
    
    function deleteDepartmentFun(id,r) {
   		if(r=="1"){
           id=$("#departmentId").val();
     	}else{
	     	if (id == undefined) {
        		var rows = departmentDataGrid.datagrid('getSelections');
        		id = rows[0].id;
	        } else {
	            departmentDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	        }
     	}
        parent.$.messager.confirm('询问', '您是否要删除当前信息？', function(b) {
            if (b) {
                progressLoad();
                $.post('${path }/admin/department/delete', {
                    id : id
                }, function(result) {
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        departmentDataGrid.datagrid('reload');
                        $("#departmentTree").tree("reload");
                    } else {
                        parent.$.messager.alert('错误', result.msg, 'error');
                    }
                    progressClose();
                }, 'JSON');
            }
        });
    }
    
    function editDepartmentFun(id,r) {
    	if(r=="1"){
           id=$("#departmentId").val();
     	}else{
	     	if (id == undefined) {
            	var rows = departmentDataGrid.datagrid('getSelections');
           		id = rows[0].id;
	        } else {
	            departmentDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	        }
     	}
        parent.addFrameTab('院系编辑','${path }/admin/department/edit?id='+id,'');
    }
    //分配用户
     function assignUserFun(id,r) {
     	if(r=="1"){
           id=$("#departmentId").val();
     	}else{
	     	 if (id == undefined) {
            	var rows = departmentDataGrid.datagrid('getSelections');
           	 	id = rows[0].id;
	        } else {
	            departmentDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	        }
     	}
     	
        $("#departmentId").val(id);
        parent.$.modalDialog({
            title : '分配用户列表',
            width : 500,
            height : 500,
            href : '${path }/admin/department/assignuser?id='+id
        });
    }
     //查看分配用户
     function assignUserList(id,r) {
     	var name=cuurname;
     	if(r=="1"){
           id=$("#departmentId").val();
     	}else{
     		var rows = departmentDataGrid.datagrid('getSelections');
     		name = rows[0].department_name;
	     	if (id == undefined) {
	            id = rows[0].id;
	        } else {
	            departmentDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	        }
     	}
        parent.$.modalDialog({
            title : ''+name,
            width : 500,
            height : 400,
            href : '${path }/admin/department/userList?id='+id
        });
    }
    function searchDepartmentFun() {
    	//$("#departmentTree").tree("reload");
        departmentDataGrid.datagrid('load', $.serializeObject($('#searchDepartmentForm')));
    }
    function cleanDepartmentFun() {
        $('#searchDepartmentForm input').val('');
        departmentDataGrid.datagrid('load', {});
    }
    
</script>
<div class="easyui-layout department" data-options="fit:true,border:false">
 <div data-options="region:'north',border:false" style="height: 30px; overflow: hidden;background-color: #fff">
        <form id="searchDepartmentForm">
       		<input name="departmentId" type="hidden" id="departmentId" value=""/>
            <table>
                <tr>
                    <th>院系名称:</th>
                    <td><input name="departmentName" placeholder="请输入院系标题"/>
                     	<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fi-magnifying-glass',plain:true" onclick="searchDepartmentFun();">查询</a>
                	</td>
                </tr>
            </table>
        </form>
    </div>
    <div data-options="region:'center',border:true,title:'信息列表'" >
        <table id="departmentDataGrid" data-options="fit:true,border:false"></table>
    </div>
    <c:choose> <c:when test="${hidden!=true}"> 
    <div data-options="region:'west',border:true,split:false,hide:true,title:'院系栏目'" refresh="reloadAll" expand="expandAll" collapse="collapseAll" style="width:150px; ">
        <ul id="departmentTree" fit="true" style="width:160px;margin: 10px 10px 10px 10px"></ul>
    </div>
    </c:when></c:choose>
    <div id="deprightmenu" class="easyui-menu" style="width:120px;">
	 	<shiro:hasPermission name="/admin/department/assignuser">
		<div onclick="assignUserFun(-1,1)" data-options="iconCls:'fi-plus '">分配用户</div>
		</shiro:hasPermission>
		<shiro:hasPermission name="/admin/department/userList">
		<div onclick="assignUserList(-1,1)" data-options="iconCls:'fi-zoom-in '">查看用户</div>
		</shiro:hasPermission>
		<shiro:hasPermission name="/admin/department/edit">
		<div onclick="editDepartmentFun(-1,1)" data-options="iconCls:'fi-pencil '">编辑</div>
		</shiro:hasPermission>
		<shiro:hasPermission name="/admin/department/delete">
		<div onclick="deleteDepartmentFun(-1,1)" data-options="iconCls:'fi-x '">删除</div>
		</shiro:hasPermission>
	</div>
</div>
<div id="departmentToolbar" style="display: none;">
    <shiro:hasPermission name="/admin/department/add">
        <a onclick="addDepartmentFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-green'">添加</a>
    </shiro:hasPermission>
</div>
